package com.yaoge.service.impl;

import com.yaoge.dao.CommodityDao;
import com.yaoge.dao.SaleRecordsDao;
import com.yaoge.domain.Commodity;
import com.yaoge.exception.NotEnoughException;
import com.yaoge.service.SaleService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.NotDirectoryException;

public class SaleServiceImpl implements SaleService {
    private CommodityDao commodityDao;
    private SaleRecordsDao saleRecordsDao;

    public CommodityDao getCommodityDao() {
        return commodityDao;
    }

    public void setCommodityDao(CommodityDao commodityDao) {
        this.commodityDao = commodityDao;
    }

    public SaleRecordsDao getSaleRecordsDao() {
        return saleRecordsDao;
    }

    public void setSaleRecordsDao(SaleRecordsDao saleRecordsDao) {
        this.saleRecordsDao = saleRecordsDao;
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {
//            NullPointerException.class, NotEnoughException.class
//    })
//    public void sale(Integer id, Integer count) {
//      int i=  commodityDao.updatePriceById(id,count);
//        Commodity commodity=commodityDao.selectCommodityById(id);
//        if (commodity==null){
//            System.out.println(new NullPointerException("没有查询到商品信息").toString());
//        }else if (commodity.getStock()<count){
//            new NotDirectoryException("库存数量不够");
//
//        }
//       int i1= saleRecordsDao.insertRecords(id,count);
//       if (i>0&&i1>0){
//           System.out.println("售卖成功");
//       }else {
//           System.out.println("售卖失败");
//       }
//
//    }
    @Override
    public void sale1(Integer id, Integer count) {

        Commodity commodity=commodityDao.selectCommodityById(id);
        if (commodity==null){
            System.out.println(new NullPointerException("没有查询到商品信息").toString());
        }else if (commodity.getStock()<count){
            new NotDirectoryException("库存数量不够");

        }
        int i=  commodityDao.updatePriceById(id,count);
        int i1= saleRecordsDao.insertRecords(id,count);
        if (i>0&&i1>0){
            System.out.println("售卖成功");
        }else {
            System.out.println("售卖失败");
        }

    }
}
